from selenium.webdriver.common.by import By

from common.utils import chromeDriver


class Home:
    url = ""
    driver = ""

    def __init__(self):
        url = "http://8.155.1.153/"
        self.driver = chromeDriver.driver
        self.driver.get(self.url)

    #进行首页元素的检查
    def HomeCheckElement(self):
        #顶部导航栏
        ##个人中心
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-center > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        ##我的商城
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-myself > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        ##我的收藏
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-favor > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        ##更多入口
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-quick > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        ##购物车
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-cart > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()
        ##消息
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.header-top > div > ul.top-nav-right > div.top-nav-items.top-nav-items-message > div > a > span")
        assert element.is_displayed()
        assert element.is_enabled()

        #商品全部分类区
        ##数码办公
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(1) > a > div > h3 > span")
        #assert element.is_displayed()
        #assert element.is_enabled()
        ##时尚服饰
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(2) > a > div > h3 > span")
        #assert element.is_displayed()
        #assert element.is_enabled()
        ##名品潮包
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(3) > a > div > h3 > span")
        #assert element.isdisplayed()
        #assert element.is_enabled()
        ##个护化妆
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(4) > a > div > h3 > span")
        #assert element.is_displayed()
        #assert element.is_enabled()
        ##珠宝手表
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(5) > a > div > h3 > span")
        #assert element.is_displayed()
        #assert element.is_enabled()
        ##运动健康
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(6) > a > div > h3 > span")
        #assert element.is_displayed()
        #assert element.is_enbaled()
        ##汽车用品
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(7) > a > div > h3 > span")
        #assert element.is_displayed()
        #assert element.is_enabled()
        ##玩具乐器
        #element = self.driver.find_element(By.CSS_SELECTOR, "#goods-category > div > div > div > ul > li:nth-child(8) > a > div > h3 > span")
        ##母婴用品
        # goods-category > div > div > div > ul > li:nth-child(9) > a > div > h3 > span
        ##生活服务
        #goods-category > div > div > div > ul > li:nth-child(10) > a > div > h3 > span
        for i in range(1, 11):
            #用 f-string 格式化字符串，将变量 i 嵌入选择器
            selector = f"goods-category > div > div > div > ul > li:nth-child({i}) > a > div > h3 > span"
            element = self.driver.find_element(By.CSS_SELECTOR, selector)
            assert element.is_displayed()
            assert element.is_enabled()

        #个人信息栏
        ##头像
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.m-baseinfo.am-flex.am-flex-items-center > a > img")
        assert element.is_displayed()
        assert element.is_enabled()
        ##待付款
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(1)")
        assert element.is_displayed()
        assert element.is_enabled()
        ##待发货
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(2)")
        assert element.is_displayed()
        assert element.is_enabled()
        ##待收货
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(3)")
        assert element.is_displayed()
        assert element.is_enabled()
        ##待评价
        element = self.driver.find_element(By.CSS_SELECTOR, "body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.mod-vip > div.member-center > a:nth-child(4)")
        assert element.is_displayed()
        assert element.is_enabled()
        ##关于我们
        ##联系我们
        ##招聘英才 body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(4) > a
        ##合作及洽谈
        ##修改收货地址
        ##商品发布
        ##会员修改个人资料
        ##会员修改密码
        ##退款申请 body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child(9) > a
        for i in range(1, 10):
            selector = f"body > div.body-content-container > div.body-content-formal-container > div.am-container.slideall > div > div.banner-news.theme-data-edit-event > ul > li:nth-child({i}) > a"
            element = self.driver.find_element(By.CSS_SELECTOR, selector)
            assert element.is_displayed()
            assert element.is_enabled()

        #搜索栏
        ##搜索框
        element = self.driver.find_element(By.CSS_SELECTOR, "#search-input")
        assert element.is_displayed()
        assert element.is_enabled()
        ##搜索按钮
        element = self.driver.find_element(By.CSS_SELECTOR, "#ai-topsearch")
        assert element.is_displayed()
        assert element.is_enabled()

        #对数码办公区元素进行检查
        ##左示图
        element = self.driver.find_element(By.CSS_SELECTOR, "#floor1 > div.am-g.am-container.floor.floor-flex > div.am-u-sm-3.aggregation-container > div > a > img")
        assert element.is_displayed()
        assert element.is_enabled()
        ##数码办公区域内展示的元素
        ##主要对其价格进行检查
        for i in range(1, 9):
            selector_now = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > strong"
            selector_prev = f"#floor1 > div.am-g.am-container.floor.floor-flex > div.goods-list > div > ul > li:nth-child({i}) > div > div > p.price.am-text-truncate > span.original-price.am-text-truncate"

            element_now = self.driver.find_element(By.CSS_SELECTOR, selector_now)
            assert element_now.is_displayed()
            text = element_now.text

            price_now = float(text.replace("￥", ""))
            assert price_now > 0

            ##商品可能没有打折，所以不能用is_displayed判定元素是否存在
            element_prev = self.driver.find_element(By.CSS_SELECTOR, selector_prev)

            flag = True
            if len(element_prev) > 0:
                element = element_prev[0]
            else:
                flag = False
                element = element_now
            text = element.text

            price_prev = float(text.replace("￥", ""))
            assert price_prev > 0

            if(price_now >= price_prev) and (flag == True):
                print(f"数码办公区第{i}个商品标注错误")








